﻿using ExpressesClass.Model;
using System;
using System.Drawing;
using System.Drawing.Printing;
using ZXing;
using ZXing.Common;

namespace ExpressesClass.IPrints
{
    public class BestPrint : IPrint
    {
        private Kddh kddh;
        private int printCur;
        private DateTime now;

        private int _12, _15, _25, _50;
        public BestPrint(Kddh kddh, int printCur, DateTime now)
        {
            this.kddh = kddh;
            this.printCur = printCur;
            this.now = now;
            _12 = 48;
            _15 = 60;
            _25 = 100;
            _50 = 200;
        }
        public void Print(string printer)
        {
            PrintDocument printDoc = new PrintDocument();
            printDoc.PrinterSettings.PrinterName = printer;
            printDoc.DefaultPageSettings.PaperSize = new PaperSize("KD Size", 290, 500);
            printDoc.PrintPage += new PrintPageEventHandler(printDoc_PrintPage); //打印事件 
            printDoc.Print();
        }
        private void printDoc_PrintPage(object sender, PrintPageEventArgs e)
        {
            Graphics g = e.Graphics;
            //设置高质量插值法   
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
            //设置高质量,低速度呈现平滑程度   
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            //消除锯齿
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

            //打印时间
            PrintPrintTime(g, 10, _12 - 13, _50, 10);
            //二维码
            PrintQRMailNo(g, _25 - 20, _12 + _12 + _12 + _12 + _25 + 3 - 20, _25 + 30, _25 + 30);
            //竖向面单号
            PrintVerMailNo(g, _50 - 30, _15, 300, 50);
            //大笔
            PrintMarkDestination(g, 10, _12 + 3, _50, _12);
            //末端分拣编码
            PrintSortingCode(g, 10, _12 + _12 + 3, _50, _12);
            //集包地
            PrintPkgCode(g, 10, _12 + _12 + _12 + 3, _50, _12);
            //收
            PrintReceive(g, 5, _12 + _12 + _12 + _12 + 10, _12, _12);
            //收件人
            PrintReceiveMan(g, 10, _12 + _12 + _12 + _12 + 3, _50 - 20, _12);
            //收件地址
            PrintReceiveAddress(g, 10, _12 + _12 + _12 + _12 + 45, _50 - 20, _12 + 20);
            //寄
            PrintSend(g, 5, _12 + _12 + _12 + _12 + _25 + 3, _12, _12);
            //寄件人
            PrintSendMan(g, 10, _12 + _12 + _12 + _12 + _25 + 3, _25, _12);
            //寄件地址
            PrintSendAddress(g, 5, _12 + _12 + _12 + _12 + _25 + 45, _25, _12 + 20);
            //备注
            PrintRemark(g, 10, _12 + _12 + _12 + _12 + _25 + _25 + 3, _50, _50);
            //位置
            PrintWz(g, 10, _12 + _12 + _12 + _12 + _25 + _25 + 60, _50, _12);
            //已检视
            PrintLook(g, _50 + 30, _12 + _12 + _12 + _12 + _25 + _25 + 60, _12, _12);
            //边框
            PrintRectangle(g);
        }
        private void PrintRectangle(Graphics g)
        {
            Pen pen = new Pen(Brushes.Black, 1);
            //横
            g.DrawLine(pen, 0, _12, _50, _12);
            g.DrawLine(pen, 0, _12 + _12, _50, _12 + _12);
            g.DrawLine(pen, 0, _12 + _12 + _12, _50, _12 + _12 + _12);
            g.DrawLine(pen, 0, _12 + _12 + _12 + _12, _50, _12 + _12 + _12 + _12);
            g.DrawLine(pen, 0, _12 + _12 + _12 + _12 + _25, _50, _12 + _12 + _12 + _12 + _25);
            g.DrawLine(pen, 0, _12 + _12 + _12 + _12 + _25 + _25, _50, _12 + _12 + _12 + _12 + _25 + _25);

            //竖
            g.DrawLine(pen, _50, _12, _50, 500);
            g.DrawLine(pen, _25, _12 + _12 + _12 + _12 + _25, _25, _12 + _12 + _12 + _12 + _25 + _25);
        }
        private void PrintPrintTime(Graphics g, int x, int y, float w, float h)
        {
            Font font = new Font("微软雅黑", 6, FontStyle.Regular);
            RectangleF rec = new RectangleF(x, y, w, h);
            g.DrawString(now.ToString("yyyy/MM/dd HH:mm:ss"), font, Brushes.Black, rec);
        }
        private void PrintVerMailNo(Graphics g, int x, int y, int w, int h)
        {
            Bitmap bitmap = new Bitmap(w, h + _15);

            EncodingOptions encodeOption = new EncodingOptions();
            encodeOption.Width = w;
            encodeOption.Height = h;
            BarcodeWriter wr = new BarcodeWriter();
            wr.Options = encodeOption;
            wr.Format = BarcodeFormat.CODE_128;
            encodeOption.PureBarcode = true;
            System.Threading.Thread.Sleep(100);
            Bitmap img = wr.Write(kddh.mailNo);

            Graphics tempG = Graphics.FromImage(bitmap);
            tempG.DrawImage(img, 0, 0);

            Font font = new Font("微软雅黑", 16, FontStyle.Regular);
            RectangleF rec = new RectangleF(_15, h, w, _15);
            tempG.DrawString(kddh.mailNo, font, Brushes.Black, rec);

            bitmap.RotateFlip(RotateFlipType.Rotate90FlipNone);
            g.DrawImage(bitmap, x, y);
        }
        private void PrintMarkDestination(Graphics g, int x, int y, float w, float h)
        {
            Font font = new Font("微软雅黑", 20, FontStyle.Bold);
            RectangleF rec = new RectangleF(x, y, w, h);
            g.DrawString(kddh.markDestination, font, Brushes.Black, rec);
        }
        private void PrintSortingCode(Graphics g, int x, int y, float w, float h)
        {
            Font font = new Font("微软雅黑", 20, FontStyle.Bold);
            RectangleF rec = new RectangleF(x, y, w, h);
            g.DrawString(kddh.sortingCode, font, Brushes.Black, rec);
        }
        private void PrintPkgCode(Graphics g, int x, int y, float w, float h)
        {
            Font font = new Font("微软雅黑", 10, FontStyle.Regular);
            RectangleF rec = new RectangleF(x, y, w, h);
            g.DrawString(kddh.pkgCode, font, Brushes.Black, rec);
        }
        private void PrintReceive(Graphics g, int x, int y, float w, float h)
        {
            RectangleF rect = new RectangleF(x, y, 20, 20);
            g.FillRectangle(Brushes.Black, rect);
            Font font = new Font("微软雅黑", 9, FontStyle.Bold);
            g.DrawString("收", font, Brushes.White, x + 1, y + 1);
        }
        private void PrintReceiveMan(Graphics g, int x, int y, float w, float h)
        {
            Font font = new Font("微软雅黑", 10, FontStyle.Bold);
            RectangleF rec1 = new RectangleF(x + 20, y, w, h);
            g.DrawString(kddh.receiveMan, font, Brushes.Black, rec1);
            RectangleF rec2 = new RectangleF(x + 20, y + 20, w, h);
            g.DrawString(kddh.receiveManPhone, font, Brushes.Black, rec2);
        }
        private void PrintReceiveAddress(Graphics g, float x, float y, float w, float h)
        {
            Font font = new Font("微软雅黑", 7, FontStyle.Regular);
            RectangleF rec = new RectangleF(x, y, w, h);
            g.DrawString("地址:" + kddh.receiveManAddress, font, Brushes.Black, rec);
        }
        private void PrintSend(Graphics g, int x, int y, float w, float h)
        {
            RectangleF rect = new RectangleF(x, y, 20, 20);
            g.FillRectangle(Brushes.Black, rect);
            Font font = new Font("微软雅黑", 9, FontStyle.Bold);
            g.DrawString("寄", font, Brushes.White, x + 1, y + 1);
        }
        private void PrintSendMan(Graphics g, int x, int y, float w, float h)
        {
            Font font = new Font("微软雅黑", 7, FontStyle.Bold);
            RectangleF rec = new RectangleF(x + 20, y, w, h);
            g.DrawString(kddh.sendMan, font, Brushes.Black, rec);
            RectangleF rec1 = new RectangleF(x + 3, y + 25, w, h);
            g.DrawString(kddh.sendManPhone, font, Brushes.Black, rec1);
        }
        private void PrintSendAddress(Graphics g, float x, float y, float w, float h)
        {
            Font font = new Font("微软雅黑", 7, FontStyle.Regular);
            RectangleF rec = new RectangleF(x, y, w, h);
            g.DrawString("地址:" + kddh.sendManAddress, font, Brushes.Black, rec);
        }
        private void PrintQRMailNo(Graphics g, int x, int y, int w, int h)
        {
            EncodingOptions encodeOption = new EncodingOptions();
            encodeOption.Width = w;
            encodeOption.Height = h;
            BarcodeWriter wr = new BarcodeWriter();
            wr.Options = encodeOption;
            wr.Format = BarcodeFormat.QR_CODE;
            encodeOption.PureBarcode = false;
            System.Threading.Thread.Sleep(100);
            Bitmap img = wr.Write(kddh.mailNo);

            g.DrawImage(img, x, y);
        }
        private void PrintRemark(Graphics g, float x, float y, float w, float h)
        {
            string str = kddh.remark;

            Font font = new Font("微软雅黑", 8, FontStyle.Regular);
            RectangleF rec = new RectangleF(x, y, w, h);
            g.DrawString("序号:" + printCur + ";" + str, font, Brushes.Black, rec);
        }
        private void PrintWz(Graphics g, float x, float y, float w, float h)
        {
            string str = kddh.wzh;

            Font font = new Font("微软雅黑", 10, FontStyle.Bold);
            RectangleF rec = new RectangleF(x, y, w, h);
            g.DrawString(str, font, Brushes.Black, rec);
        }
        private void PrintLook(Graphics g, float x, float y, float w, float h)
        {
            Font font = new Font("微软雅黑", 8, FontStyle.Regular);
            RectangleF rec = new RectangleF(x, y, w, h);
            g.DrawString("已检视", font, Brushes.Black, rec);
        }
    }
}
